<?php

class Core_Model_DbTable_Settings extends Zend_Db_Table_Abstract
{
	
	protected $_name = 'nuo_core_settings';
	
	/**
	 * get settings
	 */
	public function getSetting($name, $default = null, $site_id = null)
	{
		if (null == $site_id)
		{
			$site_id = Nuo::getSiteId();
		}
		
		$row = $this -> fetchRow($this -> select() -> where('site_id=?', (int)$site_id) -> where('name=?', (string)$name));

		if (is_object($row))
		{
			return $row -> value;
		}
		return $default;

	}

	/**
	 * set settings
	 */
	public function setSetting($name, $value, $site_id = null)
	{
		if (null == $site_id)
		{
			$site_id = Nuo::getSiteId();
		}

		$row = $this -> fetchRow($this -> select() -> where('site_id=?', (int)$site_id) -> where('name=?', (string)$name));

		if (!is_object($row))
		{
			$row = $this -> fetchNew();
			$row -> setFromArray(array(
					'site_id' => $site_id,
					'name' => $name
			));
		}

		$row -> value = $value;
		$row -> save();
	}
}
